package com.thinkfit.jy.utils;

import android.content.Context;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.common.lib.constant.EventBusEvent;
import com.jaygoo.widget.OnRangeChangedListener;
import com.jaygoo.widget.RangeSeekBar;
import com.thinkfit.jy.R;

import org.greenrobot.eventbus.EventBus;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 返回按钮（用于在第三方应用中返回本应用）
 */
public class SetTargetViewUtils implements View.OnClickListener {

    private static SetTargetViewUtils setTargetViewUtils;
    public static final int TYPE_NORMAL = 0;
    public static final int TYPE_TIME = 1;
    public static final int TYPE_DISTANCE = 2;
    public static final int TYPE_KCAL = 3;
    WindowManager windowManager;
    boolean isFist = true;
    View inflate;
    Context mContext;
    TextView tvTimeOn,tvDistanceOn,tvKcalOn,tvTimeUn,tvDistanceUn,tvKcalUn,tvOk,tvNum,tvUnit;
    ConstraintLayout clBg,clItem;
    ImageView ivReduce,ivAdd;
    RangeSeekBar rsb;
    int type = 1;
    int mProgress = 0;

    public static SetTargetViewUtils getInstance(){
        if(setTargetViewUtils == null){
            setTargetViewUtils = new SetTargetViewUtils();
        }
        return setTargetViewUtils;
    }

    public void initView(Context context){
        mContext = context;
        if(windowManager == null){
            windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        }
        if(inflate == null){
            inflate = LayoutInflater.from(context).inflate(R.layout.item_set_target, null);
            clBg = inflate.findViewById(R.id.cl_bg);
            clItem = inflate.findViewById(R.id.cl_item);
            tvTimeOn = inflate.findViewById(R.id.tv_time_on);
            tvTimeUn = inflate.findViewById(R.id.tv_time_un);
            tvDistanceOn = inflate.findViewById(R.id.tv_distance_on);
            tvDistanceUn = inflate.findViewById(R.id.tv_distance_un);
            tvKcalOn = inflate.findViewById(R.id.tv_kcal_on);
            tvKcalUn = inflate.findViewById(R.id.tv_kcal_un);
            tvOk = inflate.findViewById(R.id.tv_ok);
            tvNum = inflate.findViewById(R.id.tv_num);
            tvUnit = inflate.findViewById(R.id.tv_unit);
            ivAdd = inflate.findViewById(R.id.iv_add);
            ivReduce = inflate.findViewById(R.id.iv_reduce);
            rsb = inflate.findViewById(R.id.rsb);

            rsb.setOnRangeChangedListener(new OnRangeChangedListener() {
                @Override
                public void onRangeChanged(RangeSeekBar view, float leftValue, float rightValue, boolean isFromUser) {
                    mProgress = (int) (leftValue + 0.0001);
                    tvNum.setText(String.valueOf(mProgress));

                }

                @Override
                public void onStartTrackingTouch(RangeSeekBar view, boolean isLeft) {

                }

                @Override
                public void onStopTrackingTouch(RangeSeekBar view, boolean isLeft) {

                }
            });


            clBg.setOnClickListener(this);
            clItem.setOnClickListener(this);
            tvTimeUn.setOnClickListener(this);
            tvDistanceUn.setOnClickListener(this);
            tvKcalUn.setOnClickListener(this);
            tvOk.setOnClickListener(this);
            ivReduce.setOnClickListener(this);
            ivAdd.setOnClickListener(this);
            initOnView();
        }

        initTupe(type);



        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT
        );

        layoutParams.gravity = Gravity.CENTER;

        if(isFist){
            isFist = false;
            windowManager.addView(inflate,layoutParams);
        }else {
            inflate.setVisibility(View.VISIBLE);
        }

    }

    public void initOnView(){
        if(inflate != null){
            tvTimeOn.setVisibility(View.VISIBLE);
            tvDistanceOn.setVisibility(View.INVISIBLE);
            tvKcalOn.setVisibility(View.INVISIBLE);
        }
    }

    public void goneLayout(){
        if(windowManager != null){
            if(inflate != null){
                inflate.setVisibility(View.GONE);
            }
        }

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.cl_bg:
                goneLayout();
                break;
            case R.id.tv_ok:
                ArrayList<Integer> objects = new ArrayList<>();
                objects.add(type);
                objects.add(Integer.valueOf(tvNum.getText().toString().trim()));
                HashMap<String, Object> mMap = new HashMap<>();
                mMap.put(EventBusEvent.SET_TARGET, objects);
                EventBus.getDefault().post(mMap);
                goneLayout();
                break;
            case R.id.tv_time_un:
                type = TYPE_TIME;
                initTupe(type);
                initOnView();
                break;
            case R.id.tv_distance_un:
                type = TYPE_DISTANCE;
                initTupe(type);
                if(inflate != null){
                    tvTimeOn.setVisibility(View.INVISIBLE);
                    tvDistanceOn.setVisibility(View.VISIBLE);
                    tvKcalOn.setVisibility(View.INVISIBLE);
                }
                break;
            case R.id.tv_kcal_un:
                type = TYPE_KCAL;
                initTupe(type);
                if(inflate != null){
                    tvTimeOn.setVisibility(View.INVISIBLE);
                    tvDistanceOn.setVisibility(View.INVISIBLE);
                    tvKcalOn.setVisibility(View.VISIBLE);
                }
                break;
            case R.id.iv_add:
                resetNum(true);
                break;
            case R.id.iv_reduce:
                resetNum(false);
                break;
        }
    }

    public void showToast(Context context,boolean isShowToast){
        if(!isShowToast){
            return;
        }
        View inflater = LayoutInflater.from(context).inflate(R.layout.item_toast, null);;
        Toast toast= new Toast(context);
        toast.setGravity(Gravity.CENTER , 0, 0);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(inflater);
        toast.show();

    }

    public void resetNum(boolean isAdd){
        BigDecimal num = new BigDecimal(tvNum.getText().toString().trim());
        BigDecimal dx = BigDecimal.ONE;
        BigDecimal max = new BigDecimal("100");
        if (type == 3) {
            dx = BigDecimal.TEN;
            max = new BigDecimal("1000");
        }
        if (isAdd) {
            num = num.add(dx);
        } else {
            num = num.subtract(dx);
        }
        if (num.compareTo(BigDecimal.ZERO) < 0 || num.compareTo(max) > 0) {
            return;
        }
        rsb.setProgress(num.intValue());
        tvNum.setText(String.valueOf(num.intValue()));
    }

    public void initTupe(int type){
        if(inflate == null){
            return;
        }
        if(type == 1){
            rsb.setRange(0,100);
            tvUnit.setText(mContext.getString(R.string.app_minutes));
        }else if (type == 2){
            rsb.setRange(0,100);
            tvUnit.setText(mContext.getString(R.string.app_km));
        }else {
            rsb.setRange(0,1000);
            tvUnit.setText(mContext.getString(R.string.app_kcal));
        }
        rsb.setProgress(mProgress);
    }

    public int getTypeMaxNum(int type){
       int maxNum = 0;
        if(type == 1){
            maxNum = 100;
        }else if (type == 2){
            maxNum = 100;
        }else {
            maxNum = 1000;
        }
        return maxNum;
    }

    public void invaliView(){
        if(inflate != null){
            windowManager.removeView(inflate);
            isFist = true;
            windowManager = null;
            inflate = null;
        }
    }
}
